using System.Collections.Generic;
using UnityEngine;

public class UI_InGame : MonoBehaviour
{
    private UI_SkillSlot[] skillSlot;

    private Inventory_Player playerInventroy;
    public UI_QuestTip questTip { get; private set; }
    
    [Header("Quick Item Slots")]
    [SerializeField] private float yOffsetQuickItemParent = 150;
    public Transform quickItemOptionsParent;
    private UI_QuickItemSlotOption[] quickItemOptions;
    private UI_QuickItemSlot[] quickItemSlots;
    private void Awake()
    {
        quickItemOptions = GetComponentsInChildren<UI_QuickItemSlotOption>(true);
        questTip = GetComponentInChildren<UI_QuestTip>();
    }
    private void Start()
    {
        quickItemSlots = GetComponentsInChildren<UI_QuickItemSlot>();
        playerInventroy = FindAnyObjectByType<Inventory_Player>();

        playerInventroy.OnInventoryChange += UpdateQuickSlotUIInClick;
    }
    public UI_SkillSlot GetSkillSlotInSkillType(SkillType skillType)
    {
        skillSlot = GetComponentsInChildren<UI_SkillSlot>();
        foreach (var slot in skillSlot)
        {
            if (slot.skillType == skillType)
            {
                slot.gameObject.SetActive(true);
                return slot;
            }
        }
        return null;
    }

    //当库存物品发生发生变化时
    public void UpdateQuickSlotUIInClick()
    {
        Inventory_Item[] quickItems = playerInventroy.quickItems;

        for (int i = 0; i < quickItemSlots.Length; i++)
            quickItemSlots[i].UpdateQuickSlotUI(quickItems[i]);

        //当使用消耗品时，隐藏消耗品选择的UI，防止该UI未及时更新
        HideQuickItemOptions();
    }
    //打开使用消耗品选择选择面板
    public void OpenQuickItemOptions(UI_QuickItemSlot quickItemSlot, RectTransform targetRect)
    {
        List<Inventory_Item> items = playerInventroy.itemList.FindAll(target => target.itemData.itemType == ItemType.Consumable);
        for (int i = 0; i < quickItemOptions.Length; i++)
        {
            if (i < items.Count)
            {
                quickItemOptions[i].gameObject.SetActive(true);
                quickItemOptions[i].SetupOptionSlot(quickItemSlot, items[i]);
            }
            else
                quickItemOptions[i].gameObject.SetActive(false);
        }
        quickItemOptionsParent.position = targetRect.position + Vector3.up * yOffsetQuickItemParent;
    }

    public void HideQuickItemOptions()
    {
        foreach (var slot in quickItemOptions)
        {
            if (slot.gameObject.activeSelf)
                slot.gameObject.SetActive(false);
        }
    }
    public bool IsOpenQuickItemOption()
    {
        foreach (var slot in quickItemOptions)
        {
            if (slot.gameObject.activeSelf)
                return true;
        }
        return false;
    }

}
